<?php

/*
 * This file will perform the ajax for deleting a position.
 * This is being used by admPositionView.php.
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 28, 2011
 * 
 * In parameters: positionId and positionName
 * Out parameters: flag and msg about the success of deletion of position
 */

//********************************************************************************************************
/*
 * Define the functions in here
 */

    // Start - Check if position has connection with any user
    function noAccountDeptPos($deptPosId) {
        $query = 
        "
            SELECT *
            FROM Account_Dept_Pos            
            WHERE deptPosId = '$deptPosId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if position has connection with any user 
    
    // Start - Check if $deptPosId has connection with RouteStepDeptPos
    function noRouteStepDeptPos($deptPosId) {
        $query = 
        "
            SELECT *
            FROM RouteStep_Dept_Pos            
            WHERE deptPosId = '$deptPosId'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if $deptPosId has connection with RouteStepDeptPos
    
    // Start - Check if $deptPosId has connection with SentAttachment
    function noSentAttach($deptPosId) {
        $query = 
        "
            SELECT *
            FROM Sent_Attachment           
            WHERE receiverId = '$deptPosId' AND orgFlag = 'Internal'
        ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_fetch_array($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - Check if $deptPosId has connection with SentAttachment
    
    // Start - Delete data in Dept_Pos_SecurityGroup
    function deleteDeptPosSecGrp($deptPosId) {
        $query =
            "
                DELETE FROM Dept_Pos_SecurityGroup WHERE deptPosId = '$deptPosId';
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Delete data in Dept_Pos_SecurityGroup
    
    // Start - Delete data in Department_Position
    function deleteDeptPos($deptPosId) {
        $query =
            "
                DELETE FROM Department_Position WHERE deptPosId = '$deptPosId';
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Delete data in Department_Position
    
    // Start - Delete data in Position
    function deletePos($positionId) {
        $query =
            "
                DELETE FROM Position WHERE positionId = '$positionId';
            ";

        mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    } // End - Delete data in Position
    
    // Start - Position may be deleted if there is no other Department_Position is connected with it
    function noOtherDeptPos($positionId) {
        $query =
            "
                SELECT *
                FROM Department_Position
                WHERE positionId = '$positionId'
            ";
        
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        
        if ( mysql_num_rows($result) == 0 ) {
            return true;
        }
        else {
            return false;
        }
    } // End - // Position may be deleted if there is no other Department_Position is connected with it
    
//********************************************************************************************************

session_start();

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    include('../includes/siteConfig.php');
    
    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // Get the data from post
    $deptPosId = $_POST['deptPosId'];
    
    // Get deptPosId details
    $query =
        "
            SELECT D.departmentName as departmentName, P.positionId as positionId, P.positionName as positionName
            FROM Department_Position as DP, Department as D, Position as P
            WHERE DP.deptPosId = '$deptPosId' AND DP.departmentId = D.departmentId AND DP.positionId = P.positionId
        ";

    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row = mysql_fetch_array($result);
    $departmentName = $row['departmentName'];
    $positionId = $row['positionId'];
    $positionName = $row['positionName'];
    
    if ( noAccountDeptPos($deptPosId) == true && noRouteStepDeptPos($deptPosId) == true && noSentAttach($deptPosId) == true ) {
        // Can be edited or deleted if no data in:
        // * No data in Account_Dept_Pos
        // * No data in RouteStep_Dept_Pos
        // * No data in Sent_Attachment
        
        deleteDeptPosSecGrp($deptPosId);
        deleteDeptPos($deptPosId);
        
        // Position may be deleted if there is no other Department_Position is connected with it
        if ( noOtherDeptPos($positionId) == true ) {
            deletePos($positionId);
        }

        $jsondata = array();
        $jsondata['msg'] = $positionName . " of ".$departmentName." department was successfully deleted.";
        $jsondata['flag'] = "success";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    else {
        $jsondata = array();
        $jsondata['msg'] = "Error : Ajax in deleting the position was not successful.";
        $jsondata['flag'] = "error";
        $feed[] = $jsondata;
        echo json_encode($feed);
    }
    
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
    echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
